package org.team4.util;

import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

/**
 * <p>
 * 程序名称： SmtpHelper.java
 * </p>
 * <p>
 * 程序说明： 用于发送邮件
 * </p>
 * <p>
 * 版权信息： Copyright 深圳市维远泰克科技有限公司
 * </p>
 * <p>
 * 时间： Nov 25, 2010 9:05:22 AM
 * </p>
 * 
 * @author： Marker
 * @version： Ver 0.1
 */
public class SmtpHelper {
	private Session session; // 邮件会话对象
	private Properties props; // 系统属性
	private String username = ""; // smtp认证用户名和密码
	private String password = "";
	private Multipart mp; // Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象
	private Message mimeMsg;

	public SmtpHelper(String smtp) {
		setSmtpHost(smtp);
		createMimeMessage();
	}

	/**
	 * 设置SMTP主机
	 * 
	 * @param hostName
	 */
	public void setSmtpHost(String hostName) {
		if (props == null)
			props = System.getProperties(); // 获得系统属性对象
		props.put("mail.smtp.host", hostName); // 设置SMTP主机
	}

	/**
	 * 创建MIME邮件对象
	 */
	public void createMimeMessage() {
		session = Session.getDefaultInstance(props, null); // 获得邮件会话对象
		mimeMsg = new MimeMessage(session); // 创建MIME邮件对象
		mp = new MimeMultipart();
	}

	/**
	 * 是否需要身份验证
	 * 
	 * @param need
	 */
	public void setNeedAuth(boolean need) {
		System.out.println("设置smtp身份认证：mail.smtp.auth = " + need);
		if (props == null)
			props = System.getProperties();
		if (need) {
			props.put("mail.smtp.auth", "true");
		} else {
			props.put("mail.smtp.auth", "false");
		}
	}

	/**
	 * 设置用户名密码
	 * 
	 * @param name
	 * @param pass
	 */
	public void setNamePass(String name, String pass) {
		username = name;
		password = pass;
	}

	/**
	 * 设置邮件主题
	 * 
	 * @param mailSubject
	 * @throws Exception
	 */
	public void setSubject(String mailSubject) throws Exception {
		mimeMsg.setSubject(mailSubject);
	}

	/**
	 * 设置邮件内容
	 * 
	 * @param mailBody
	 * @throws Exception
	 */
	public void setBody(String mailBody) throws Exception {
		mimeMsg.setText(mailBody);
		BodyPart messageBodyPart = new MimeBodyPart();
		messageBodyPart.setText(mailBody);
		messageBodyPart.setContent("<meta http-equiv=Content-Type content=text/html; charset=UTF-8>" + mailBody, "text/html;charset=UTF-8");
		mp.addBodyPart(messageBodyPart);
	}

	/**
	 * 添加附件
	 * 
	 * @param filename
	 */
	public void addFileAffix(String filePath, String fileName) throws Exception {
		BodyPart bp = new MimeBodyPart();
		FileDataSource fileds = new FileDataSource(filePath);
		bp.setDataHandler(new DataHandler(fileds));
		bp.setFileName(MimeUtility.encodeWord(fileName, "UTF-8", null));
		mp.addBodyPart(bp);
	}

	/**
	 * 设置发件人
	 * 
	 * @param from
	 * @throws Exception
	 */
	public void setFrom(String from) throws Exception {
		mimeMsg.setFrom(new InternetAddress(from)); // 设置发信人
	}

	/**
	 * 设置发送时间
	 * 
	 * @param date
	 * @throws Exception
	 */
	public void setSendTime(Date date) throws Exception {
		mimeMsg.setSentDate(date);
	}

	/**
	 * 设置收件人
	 * 
	 * @param to
	 * @throws Exception
	 */
	public void setTo(String to) throws Exception {
		mimeMsg.setRecipients(Message.RecipientType.TO, (Address[]) InternetAddress.parse(to));
	}

	/**
	 * 设置抄送
	 * 
	 * @param copyto
	 * @throws Exception
	 */
	public void setCopyTo(String copyto) throws Exception {
		mimeMsg.setRecipients(Message.RecipientType.CC, (Address[]) InternetAddress.parse(copyto));
	}

	/**
	 * 添加密送
	 * 
	 * @param blindCopy
	 * @throws Exception
	 */
	public void setBlindCopyTo(String blindCopy) throws Exception {
		mimeMsg.setRecipients(Message.RecipientType.BCC, (Address[]) InternetAddress.parse(blindCopy));
	}

	/**
	 * 发送
	 * 
	 * @throws Exception
	 */
	public void sendout() throws Exception {
		mimeMsg.setContent(mp);
		mimeMsg.saveChanges();
		System.out.println("正在发送邮件....");
		Session mailSession = Session.getInstance(props, null);
		Transport transport = mailSession.getTransport("smtp");
		transport.connect((String) props.get("mail.smtp.host"), username, password);
		transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.TO));
		if (mimeMsg.getRecipients(Message.RecipientType.CC) != null)
			transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.CC));
		if (mimeMsg.getRecipients(Message.RecipientType.BCC) != null)
			transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.BCC));
		System.out.println("发送邮件成功！");
		transport.close();
	}

	/**
	 * 测试连接是否成功
	 * 
	 * @return
	 */
	public boolean testConnect() {
		try {
			Session mailSession = Session.getInstance(props, null);
			Transport transport = mailSession.getTransport("smtp");
			transport.connect((String) props.get("mail.smtp.host"), username, password);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
}
